package cn.gpnusz.oliverwiki.controller;

import cn.gpnusz.oliverwiki.req.CategorySaveReq;
import cn.gpnusz.oliverwiki.resp.CommonResp;
import cn.gpnusz.oliverwiki.resp.CategoryQueryResp;
import cn.gpnusz.oliverwiki.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @description 电子书分类分类Controller
 * @author h0ss
 * @date 2021/9/6 13:15 
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 展示所有电子书分类数据
     *
     * @return : cn.gpnusz.oliverwiki.resp.CommonResp<cn.gpnusz.oliverwiki.resp.PageResp<cn.gpnusz.oliverwiki.resp.CategoryQueryResp>>
     * @author h0ss
     */
    @RequestMapping("/list")
    public CommonResp<List<CategoryQueryResp>> list() {
        CommonResp<List<CategoryQueryResp>> resp = new CommonResp<>();
        resp.setMessage("数据获取成功!");
        resp.setContent(categoryService.getAll());
        return resp;
    }


    /**
     * 新增/编辑电子书分类
     *
     * @param categorySaveReq :
     * @return : cn.gpnusz.oliverwiki.resp.CommonResp<java.lang.Object>
     * @author h0ss
     */
    @PostMapping("/save")
    public CommonResp<Object> save(@Valid @RequestBody CategorySaveReq categorySaveReq) {
        categoryService.save(categorySaveReq);
        return new CommonResp<>();
    }

    /**
     * 根据id删除电子书分类
     *
     * @param id :
     * @return : cn.gpnusz.oliverwiki.resp.CommonResp<java.lang.Object>
     * @author h0ss
     */
    @DeleteMapping("/delete/{id}")
    public CommonResp<Object> delete(@PathVariable Long id) {
        categoryService.delete(id);
        return new CommonResp<>();
    }


}
